package com.example.ljw.frescowallpaper.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;


import com.example.ljw.frescowallpaper.R;
import com.example.ljw.frescowallpaper.adapter.MySortDetailsAdapter;
import com.example.ljw.frescowallpaper.bean.SortDetailsBean;
import com.example.ljw.frescowallpaper.config.URLConfig;
import com.example.ljw.frescowallpaper.utils.IMyRetrofit;
import com.example.ljw.frescowallpaper.widget.DividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;



public class SortDetailActivity extends AppCompatActivity {
    private int mCurpage = 1;
    private int mLastVisibleItem = 0;
    private int limit=30;
    private String id=null;
    private ProgressBar progressBar;
    //MAP
    private Map<String,String> map=new HashMap<>();
    //适配器
    private MySortDetailsAdapter mAdapter;
    //retrofit
    private Retrofit mRetrofit=null;
    //
    private Context mContext=this;
    //
    private RecyclerView recyclerView_sortdetail;
    //数据源
    private List<SortDetailsBean.ResBean.DataBean> mList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sort_detail);
        progressBar= (ProgressBar) findViewById(R.id.progressBar_fragment_detail);
        progressBar.setVisibility(View.VISIBLE);
        Intent intent=getIntent();
         id=intent.getStringExtra("id");
        String name =intent.getStringExtra("name");
        initMap();
        initView(name);
        initRecyclerView();
        initRetrofit();
        loadNetData(map);
      
    }

    private void initMap() {
        map.put(URLConfig.URLSort.ID,id);
        map.put(URLConfig.URLSort.HEIGHT,"1334");
        map.put(URLConfig.URLSort.LIMIT,limit+"");
        map.put(URLConfig.URLSort.SKIP,"0");
        map.put(URLConfig.URLSort.SYS_LANGUAGE,"zh-Hans-CN&width=750");


    }

    private void loadNetData(final Map<String,String> map) {
        //根据传过来的id去加载数据
        IMyRetrofit iMyRetrofit=mRetrofit.create(IMyRetrofit.class);
        Observable observable=iMyRetrofit.getSortDeatailsObseravle(map);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SortDetailsBean>() {
                    @Override
                    public void onCompleted() {
                        progressBar.setVisibility(View.INVISIBLE);
                        recyclerView_sortdetail.scrollToPosition(mLastVisibleItem);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(SortDetailsBean sortDetailsBean) {
                        mList.clear();
                        Toast.makeText(mContext, "细节数据加载成功", Toast.LENGTH_SHORT).show();
                        mList=sortDetailsBean.getRes().getData();
                        //设置布局管理器
                        final GridLayoutManager gridLayoutManager=new GridLayoutManager(mContext,2);
                        recyclerView_sortdetail.setLayoutManager(gridLayoutManager);
                        //设置适配器
                        mAdapter=new MySortDetailsAdapter(mContext,mList);
                        recyclerView_sortdetail.setAdapter(mAdapter);
                        recyclerView_sortdetail.addOnScrollListener(new RecyclerView.OnScrollListener() {
                            @Override
                            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                                super.onScrollStateChanged(recyclerView, newState);
                                if(newState==RecyclerView.SCROLL_STATE_IDLE&&
                                        mLastVisibleItem==mAdapter.getItemCount()-1){
                                    recyclerView.postDelayed(null,1);
                                    limit=limit+30;
                                    progressBar.setVisibility(View.VISIBLE);
                                    initMap();
                                    loadNetData(map);
                                }


                            }

                            @Override
                            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                                super.onScrolled(recyclerView, dx, dy);
                                if(gridLayoutManager.findLastVisibleItemPosition()!=-1) {
                                    mLastVisibleItem = gridLayoutManager.findLastVisibleItemPosition();
                                }
                                }
                        });

                    }


                });

    }

    private void initRetrofit() {
        //初始化retrofit 传入URL地址
        mRetrofit=new Retrofit.Builder()
                .baseUrl(URLConfig.URL_SORT_BASE)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }

    private void initRecyclerView() {
        //初始化recyclerView
        //设置recycleView
        recyclerView_sortdetail.setHasFixedSize(true);
        recyclerView_sortdetail.setItemAnimator(new DefaultItemAnimator());
        recyclerView_sortdetail.addItemDecoration(new DividerItemDecoration(mContext,
                DividerItemDecoration.VERTICAL_LIST));

    }

    private void initView(String name) {
        //设置toolbar支持actionbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle(name);
        //recyclerView初始化
        recyclerView_sortdetail= (RecyclerView) findViewById(R.id.recyclerView_sortdetail);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

}
